package com.efast.cafe.portal.controller.common;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.portal.bean.common.AliyunSTSBean;
import com.efast.cafe.portal.bean.common.AttachmentBean;
import com.efast.cafe.portal.bean.common.FileUploadReturnBean;
import com.efast.cafe.portal.entity.common.PortalAttachment;
import com.efast.cafe.portal.service.common.IPortalAttachmentService;
import com.efast.cafe.util.AliyunOSSUtil;

@Controller
@RequestMapping("/common")
public class AliyunSTSController {
	@Autowired
	private IPortalAttachmentService portalAttachmentService;
	
	@RequestMapping(value = "/temp/token")
	public @ResponseBody ResultBean tempToken(HttpServletResponse response, HttpServletRequest request) {

		ResultBean result = new ResultBean(true, "");	
		try {
			result.setObject(AliyunOSSUtil.STSTempToken());
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("获取token失败");
			e.printStackTrace();
		}
		return result;

	}
	
	
	@RequestMapping(value = "/temp/download")
	public @ResponseBody ResultBean tempDownload(
			HttpServletResponse response, HttpServletRequest request) {

		ResultBean result = new ResultBean(true, "");		
		try {
			String time="3600";
			result.setObject(portalAttachmentService.queryTempDownloadUrl(2187, time));
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("下载失败");
			e.printStackTrace();
		}
		return result;

	}
	
	@RequestMapping(value = "/temp/upload")
	public @ResponseBody ResultBean tempUpload(
			@RequestBody AttachmentBean attBean,
			HttpServletResponse response, HttpServletRequest request) {

		ResultBean result = new ResultBean(true, "");		
		try {
			AliyunSTSBean bean=new AliyunSTSBean();
			List<AttachmentBean> attachments = new ArrayList<>();
			attachments.add(attBean);
			bean.setAttachments(attachments);
			List<FileUploadReturnBean> attrids=new ArrayList<>();
			if(bean.getAttachments()!=null&&bean.getAttachments().size()>0){
				List<PortalAttachment> atts = new ArrayList<>();
				for(int i=0;i<bean.getAttachments().size();i++){
					PortalAttachment attr = new PortalAttachment();
					
					attr.setFilePath(bean.getAttachments().get(i).getFilePath());
					
					attr.setIsDelete(0);
					String sysName=bean.getAttachments().get(i).getSysName();
					String realName=sysName.substring(sysName.indexOf("-")+1);
					attr.setRealName(realName);
					attr.setSysName(sysName);
					atts.add(attr);
				}
				attrids = portalAttachmentService.saveAttrachment(atts);
			}	                 
			result.setObject(attrids.get(0));
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("上传数据保存失败");
			e.printStackTrace();
		}
		return result;

	}


}



